package com.zjdiepu.www.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zjdiepu.www.base.core.BaseEnum;
import com.zjdiepu.www.base.core.BaseException;

/**
 * 类名称：ExceptionHelper
 * 类描述：构建异常工具类
 * @author xiongxiaotun
 * @version 1.0
 */
public final class ExceptionHelper {
    private static final Logger log = LoggerFactory.getLogger(ExceptionHelper.class);
    
    public static void thow(BaseEnum baseEnum) {
    	thow(baseEnum, null);
    }

    public static void thow(BaseEnum baseEnum, Exception exceptionSource) {
    	thow(baseEnum.getCode().toString(), baseEnum.getValue().toString(), exceptionSource);
    }
    
    public static void thow(String errorCode) {
    	thow(errorCode, (String)null);
    }
    
    public static void thow(String errorCode, String errorMsg) {
    	thow(errorCode, errorMsg, null);
    }
    
    public static void thow(String errorCode, Exception exceptionSource) {
    	thow(errorCode, null, exceptionSource);
    }
    
    public static void thow(String errorCode, String errorMsg, Exception exceptionSource) {
    	throw new BaseException(errorCode, errorMsg, exceptionSource);
    }
    
    /**
     * 打印异常
     * @author xiongxiaotun
     * @date 2017年3月23日
     * @since v1.0.0
     * @param exception
     * @param logger
     */
    public static void logBaseException(BaseException exception, Logger logger) {
        if (logger == null) {
            logger = log;
        }
        
        if(StringUtils.isNotBlank(exception.getErrorCode())) {
        	logger.error("[错误代码：{}]",exception.getErrorCode());
        }
        
        if(StringUtils.isNotBlank(exception.getErrorMsg())) {
        	logger.error("[错误消息：{}]",exception.getErrorMsg());
        }

        if (ObjectUtil.isNotEmpty(exception.getErrorSource())) {
            logger.error(" [错误源： {} ]" ,exception.getErrorSource());
        }
    }

}